﻿#include "xml.h"

#include <gtest/gtest.h>

TEST(XmlParseTest, single_element_with_attr)
{
	const xml::Elements answer{ {"abc", {{"a", "fuck"}, {"b", "world"}}, {}} };
	EXPECT_EQ(xml::ParseString(R"(<abc a="fuck" b="world"/>)"), answer);
	EXPECT_EQ(xml::ParseString(R"(<abc a="fuck" b="world"></abc>)"), answer);
}

TEST(XmlParseTest, with_sub_element)
{
	const xml::Elements answer{ {"abc", {{"a", "fuck"}, {"b", "world"}}, {{"c", {}, {}}}} };
	EXPECT_EQ(xml::ParseString(R"(<abc a="fuck" b="world"><c/></abc>)"), answer);
	EXPECT_EQ(xml::ParseString(R"(<abc a="fuck" b="world"><c></c></abc>)"), answer);
}

TEST(XmlParseTest, sub_element_is_text)
{
	const xml::Elements answer =
	{ {"abc", {{"a", "fuck"}, {"b", "world"}}, {{"c", {}, {{"c_text", {}, {}, xml::ElemType::Text}}}}} };
	EXPECT_EQ(xml::ParseString(R"(<abc a="fuck" b="world"><c>c_text</c></abc>)"), answer);
}

TEST(XmlParseTest, multi_sub_element)
{
	const xml::Elements answer = {
		{"abc", {{"a", "fuck"}, {"b", "world"}}, {{"abc_text", {}, {}, xml::ElemType::Text}, {"c", {}, {{"c_text", {}, {}, xml::ElemType::Text}}}}} };
	EXPECT_EQ(xml::ParseString(R"(<abc a="fuck" b="world">abc_text<c>c_text</c></abc>)"), answer);
}

TEST(XmlParseTest, no_end)
{
	EXPECT_THROW(xml::ParseString(R"(<abc a="fuck" b="world">)"), std::runtime_error);
	EXPECT_THROW(xml::ParseString(R"(<abc a="fuck" b="wor)"), std::runtime_error);
}

TEST(XmlParseTest, error_end)
{
	// 前后标签不匹配
	EXPECT_THROW(xml::ParseString(R"(<abc a="fuck" b="world"></abcd>)"), std::runtime_error);
	EXPECT_THROW(xml::ParseString(R"(<abc a="fuck" b="world"><c></c1></abc>)"), std::runtime_error);
}